﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
using System.Collections.Generic;
public partial class Admin_adminQuestionClass : System.Web.UI.Page
{
    QuestionClassBll qcBll = new QuestionClassBll();
    QuestionBll qBll = new QuestionBll();
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"]!=null)
                {
                    string action = Request.QueryString["action"].ToString().Trim();
                    switch (action)
                    {
                        case "Insert":
                            Insert();
                            break;
                        case "delAll":
                            Delete();
                            break;
                        case "editAll":
                            Edit(Request.QueryString["id"]);
                            break;
                        case "MoveAll":
                            move();
                        break;
                        default:
                            break;
                    }
                }
            }
            BindFQuestionClass();
        }
    }
    #region 删除
    public void Delete() 
    {
        int Qcid=Convert.ToInt32(Request.QueryString["id"]);
        int count = Convert.ToInt32(Request.QueryString["count"]);
        if (qcBll.GetModelByFid(Qcid).Count > 0)
        {
            Response.Write("1");
            Response.End();
        }
        else
        {
            if (count < 1)
            {
                if (qcBll.Delete(Qcid))
                {

                }
                else
                    return;
            }
            else 
            {
                Response.Write("2");
                Response.End();
            }
        }
    }
    #endregion

    #region Ajax批量修改
    public void Edit(string id) 
    {
        try
        {
            if (id.Contains(","))
            {
                lock (objSync)
                {
                    string[] idParam = id.Split(',');
                    for (int i = 0; i < idParam.Length; i++)
                    {
                        QuestionClass qc = new QuestionClass();
                        qc.questionClassID = Convert.ToInt32(idParam[i]);
                        qc.questionClassName = Request.Form["qcname" + idParam[i] + ""].ToString();
                        qc.questionFid =Convert.ToInt32(Request.Form["qcfid" + idParam[i] + ""]);
                        qc.Questionstate = Convert.ToBoolean(Request.Form["qs" + idParam[i]]);
                        if (qcBll.Update(qc))
                        {

                        }
                        else
                            return;
                    }
                }
            }
            else 
            {
                QuestionClass qc = new QuestionClass();
                qc.questionClassID = Convert.ToInt32(id);
                qc.questionClassName = Request.Form["qcname" + id].ToString();
                qc.questionFid=Convert.ToInt32(Request.Form["qcfid"+id]);
                qc.Questionstate = Convert.ToBoolean(Request.Form["qs"+id]);
                if (qcBll.Update(qc))
                {

                }
                else
                    return;
            }
        }
        catch (Exception)
        {
            
            throw;
        }
    }
    #endregion

    #region ajax批量移动
    public void move()
    {
        string qcids = Request.QueryString["qcid"].ToString();
        string fid = Request.QueryString["id"].ToString();
        string fids = Request.QueryString["fids"].ToString();
        if (fids.Contains("0"))
        {
            Response.Write("2");
            Response.End();
        }
        if (!qcids.Contains(fid))
        {
            try
            {
                if (qcids.Contains(","))
                {
                    string[] idParam = qcids.Split(',');                  
                    if (qcBll.Update(idParam, fid))
                    {

                    }
                }
                else
                {
                    string[] idParam ={ qcids };
                    if (qcBll.Update(idParam, fid))
                    {

                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        else 
        {
            Response.Write("1");
            Response.End();
        }
    }
    #endregion

    #region 添加问题分类
    public void Insert() 
    {
        QuestionClass qc = new QuestionClass();
        qc.questionClassName = Request.Form["questionclassname"].ToString();
        qc.questionFid =Convert.ToInt32(Request.QueryString["id"]);
        qc.Questionstate = Convert.ToBoolean(Request.Form["isConProduct"]);
        if (qc.questionClassName == "")
        {
            Response.Write("1");
            Response.End();
        }
        else
            qcBll.Insert(qc);
    }
    #endregion

    #region 绑定父问题分类
    public void BindFQuestionClass() 
    {
        IList<QuestionClass> qcList = qcBll.GetModelByFid(0);
        this.Repeater1.DataSource = qcList;
        this.Repeater1.DataBind();
    }
    #endregion

    #region 根据问题类别id得到与该类别相关的问题
    public int getCountByID(Object questionClassID)
    {
       return qBll.getCountByQcID(Convert.ToInt32(questionClassID));
    }
    #endregion

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rep = e.Item.FindControl("Repeater2") as Repeater;//找到里层的repeater对象
            QuestionClass qc = (QuestionClass)e.Item.DataItem;//找到分类Repeater关联的数据项 
            int qClassID = Convert.ToInt32(qc.questionClassID); //获取填充子类的id 
            rep.DataSource = qcBll.GetModelByFid(qClassID);
            rep.DataBind();
        }
    }

    #region 是否选中
    public string IsExistCheck(object obBool) 
    {
        if (obBool.ToString()=="True")
        {
            return "checked=\"checked\"";
        }
        return "";
    }
    #endregion 

    #region 自定义下拉框
    public string QuestionClassDp()
    {
        StringBuilder str = new StringBuilder();
        IList<QuestionClass> qcList = qcBll.GetModelByFid(0);
        str.Append("<option value='0'>--选择问题类别--</option>");
        foreach (QuestionClass qc in qcList)
        {
            str.Append("<option value='" + qc.questionClassID + "'>"+qc.questionClassName+"</option>");
        }
        return str.ToString();
    }
    #endregion
   
}
